<template>
  <div class="login-container">
    <div class="login-box">
      <h1 class="title">注册账户<br/>开启新世界</h1>
      
      <form class="login-form" @submit.prevent="handleRegister">
        <div class="form-group">
          <label class="form-label">用户名</label>
          <input 
            type="text" 
            v-model="username"
            placeholder="请输入用户名"
            class="form-input"
          />
        </div>

   

        <div class="form-group">
          <label class="form-label">密码</label>
          <input 
            type="password" 
            v-model="password"
            placeholder="请输入密码"
            class="form-input"
          />
        </div>

        <div class="form-group">
          <label class="form-label">确认密码</label>
          <input 
            type="password" 
            v-model="confirmPassword"
            placeholder="请再次输入密码"
            class="form-input"
          />
        </div>

        <div v-if="errorMessage" class="error-message">
          {{ errorMessage }}
        </div>

        <button 
          type="submit" 
          class="sign-in-btn" 
          :disabled="loading"
        >
          {{ loading ? '注册中...' : '立即注册' }}
        </button>
        
        <p class="register-hint">
          已有账户？ <a href="#" class="register-link" @click.prevent="goToLogin">返回登录</a>
        </p>

        <div class="divider">
          <span>或使用以下方式注册</span>
        </div>

        <div class="social-login">
          <button class="social-btn google">
            <img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/google/google-original.svg" alt="Google" />
          </button>
          <button class="social-btn apple">
            <img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/apple/apple-original.svg" alt="Apple" />
          </button>
          <button class="social-btn facebook">
            <img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/facebook/facebook-original.svg" alt="Facebook" />
          </button>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

// 创建 axios 实例
const api = axios.create({
  baseURL: 'https://fogpdtpvfoer.sealoshzh.site',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  },
  withCredentials: false
})

export default {
  name: 'RegisterView',
  data() {
    return {
      username: '',
      password: '',
      confirmPassword: '',
      loading: false,
      errorMessage: ''
    }
  },
  methods: {
    async handleRegister() {
      // 清除之前的错误信息
      this.errorMessage = ''
      
      // 表单验证
      if (!this.username || !this.password || !this.confirmPassword) {
        this.errorMessage = '请填写所有必填项'
        return
      }
      
      if (this.password !== this.confirmPassword) {
        this.errorMessage = '两次输入的密码不一致'
        return
      }
      
      try {
        this.loading = true
        const response = await api.post('/api/auth/register', {
          username: this.username,
          password: this.password
        })
        
        console.log('注册成功:', response.data)
        // 注册成功后跳转到登录页
        this.$router.push('/')
      } catch (error) {
        console.error('注册失败:', error)
        // 更详细的错误处理
        if (error.response) {
          // 服务���返回了错误状态码
          switch (error.response.status) {
            case 400:
              this.errorMessage = '用户名或密码格式不正确'
              break
            case 409:
              this.errorMessage = '用户名已被注册'
              break
            default:
              this.errorMessage = error.response.data.message || '注册失败，请稍后重试'
          }
        } else if (error.request) {
          // 请求发出去了但没有收到响应
          this.errorMessage = '服务器无响应，请检查网络连接'
        } else {
          // 请求配置出错
          this.errorMessage = '注册失败，请稍后重试'
        }
        console.log('详细错误信息:', error)
      } finally {
        this.loading = false
      }
    },
    goToLogin() {
      this.$router.push('/')
    }
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1a2980 0%, #26d0ce 100%);
  background-size: cover;
  padding: 20px;
  position: relative;
}

.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.2);
  pointer-events: none;
}

.login-box {
  background: rgba(255, 255, 255, 0.95);
  padding: 40px;
  border-radius: 20px;
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.2);
  width: 100%;
  max-width: 480px;
  position: relative;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.title {
  font-size: 2.5rem;
  font-weight: bold;
  margin-bottom: 30px;
  line-height: 1.2;
  background: linear-gradient(45deg, #4a90e2, #67b8e3);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 2px 4px rgba(0,0,0,0.1);
  text-align: center;
}

.form-group {
  margin-bottom: 24px;
}

.form-label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-size: 0.9rem;
  font-weight: 500;
}

.form-input {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  font-size: 16px;
  background: rgba(255, 255, 255, 0.9);
  transition: all 0.3s ease;
}

.form-input:focus {
  border-color: #4a90e2;
  box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.1);
  outline: none;
}

.form-input::placeholder {
  color: #999;
}

.sign-in-btn {
  width: 100%;
  padding: 14px;
  background: linear-gradient(45deg, #4a90e2, #67b8e3);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  margin: 10px 0 20px;
  transition: all 0.3s ease;
}

.sign-in-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(74, 144, 226, 0.3);
}

.register-hint {
  text-align: center;
  color: #666;
  margin: 20px 0;
  font-size: 0.9rem;
}

.register-link {
  color: #4a90e2;
  text-decoration: none;
  font-weight: 600;
  margin-left: 5px;
}

.register-link:hover {
  text-decoration: underline;
}

.divider {
  text-align: center;
  margin: 24px 0;
  position: relative;
}

.divider::before,
.divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 35%;
  height: 1px;
  background: rgba(0, 0, 0, 0.1);
}

.divider::before {
  left: 0;
}

.divider::after {
  right: 0;
}

.divider span {
  background: white;
  padding: 0 15px;
  color: #666;
  font-size: 0.9rem;
}

.social-login {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin-top: 20px;
}

.social-btn {
  width: 44px;
  height: 44px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  background: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  transition: all 0.3s ease;
}

.social-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.social-btn img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.error-message {
  color: #ff4d4f;
  font-size: 14px;
  margin-bottom: 16px;
  text-align: center;
  padding: 8px;
  background-color: #fff2f0;
  border: 1px solid #ffccc7;
  border-radius: 8px;
}

.sign-in-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
}

.sign-in-btn:disabled:hover {
  box-shadow: none;
  transform: none;
}
</style> 